﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tender.Tool.View
{
    public partial class UserControl2 : UserControl
    {
        private List<XmlList> lists { get; set; }
        public UserControl2()
        {
            InitializeComponent();
            uiComboBox1.Items.Add("没有审核历史");
            uiComboBox1.Items.Add("当前单据不是审核中状态");
            uiComboBox1.Items.Add("审核报错");
            uiComboBox1.Items.Add("审核完成状态还是审核中");
            //uiComboBox2.Items.Add("Ted_Tender");
            //uiComboBox2.Items.Add("Ted_ZhaoBiaoBaoMinAudit");
            //uiComboBox2.Items.Add("Ted_ZhaobiaoPingbiaojieguo");
            //uiComboBox2.Items.Add("Equip_Run");
            //uiComboBox2.Items.Add("Equip_LeaseAcceptance");

            if (File.Exists($"{System.Environment.CurrentDirectory}\\XmlListdata.xml"))
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(List<XmlList>));
                using (Stream stream = File.OpenRead($"{System.Environment.CurrentDirectory}\\XmlListdata.xml"))
                {
                    List<XmlList> newObj = (List<XmlList>)serializer.ReadObject(stream);
                    foreach (var item in newObj)
                    {
                        if (item.type == 1)
                        {
                            uiComboBox3.Items.Add(item.Name);
                        }
                        else
                        {
                            uiComboBox2.Items.Add(item.Name);
                        }
                    }
                }
            }
            uiComboBox2.Items.Add("新增");
            uiComboBox3.Items.Add("新增");
            uiComboBox2.SelectedIndexChanged += UiComboBox2_SelectedIndexChanged;
            uiComboBox3.SelectedIndexChanged += UiComboBox3_SelectedIndexChanged;
        }

        private void UiComboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (uiComboBox2.Text == "新增")
            {

                VerifyMessage verifyMessage = new VerifyMessage();
                if (verifyMessage.ShowDialog()==DialogResult.OK)
                {
                    Addedmessage addedmessage = new Addedmessage(Callback2);
                    addedmessage.ShowDialog();
                }       
            }
        }

        private void Callback2(string arg1, string arg2,string arg3)
        {
            int tender = 0;
            int.TryParse(arg3, out tender);
            lists = xmlLists();
            lists.Add(new XmlList() { Name = arg1, Value = arg2, type = 2 ,Tender= tender });
            DataContractSerializer serializer = new DataContractSerializer(typeof(List<XmlList>));
            using (Stream stream = File.Create("XmlListdata.xml"))
            {
                serializer.WriteObject(stream, lists);
            }
            foreach (var item in lists.Where(t => t.type == 2))
            {
                if(!uiComboBox2.Items.Contains(item.Name))
                uiComboBox2.Items.Insert(uiComboBox2.Items.Count - 1, item.Name);
            }
        }

        private void UiComboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (uiComboBox3.Text == "新增")
            {
                VerifyMessage verifyMessage = new VerifyMessage();
                if (verifyMessage.ShowDialog() == DialogResult.OK)
                {
                    Addedmessage addedmessage = new Addedmessage(Callback1);
                    addedmessage.ShowDialog();
                }
            }
        }

        private void Callback1(string arg1, string arg2, string arg3)
        {
             int tender = 0;
            int.TryParse(arg3, out tender);
            lists = xmlLists();
            lists.Add(new XmlList() { Name=arg1,Value=arg2,type=1, Tender = tender });
            DataContractSerializer serializer = new DataContractSerializer(typeof(List<XmlList>));
            using (Stream stream = File.Create("XmlListdata.xml"))
            {
                serializer.WriteObject(stream, lists);
            }
            foreach (var item in lists.Where(t=>t.type==1))
            {
                if(!uiComboBox3.Items.Contains(item.Name))
                uiComboBox3.Items.Insert(uiComboBox3.Items.Count-1, item.Name);
            }
        }

        /// <summary>
        ///
        /// </summary>
        public Action<string,int,string,string> FiowActionclick { get; set; }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            if(lists==null)lists= xmlLists();
            FiowActionclick?.Invoke(uiTextBox1.Text.Trim(),uiComboBox1.FindString(uiComboBox1.Text),lists.Single(t=>t.Name==uiComboBox2.Text.Trim()).Value, lists.Single(t=>t.Name== uiComboBox3.Text.Trim()).Value);
        }
        /// <summary>
        /// 读取Xml实体
        /// </summary>
        /// <returns></returns>
        private List<XmlList> xmlLists()
        {
            string str = System.Environment.CurrentDirectory;
            if (!File.Exists($"{str}\\XmlListdata.xml"))return new List<XmlList>();
                DataContractSerializer serializer = new DataContractSerializer(typeof(List<XmlList>));
            using (Stream stream = File.OpenRead($"{str}\\XmlListdata.xml"))
            {
                List<XmlList> newObj = (List<XmlList>)serializer.ReadObject(stream);
                return newObj;
            }
        }
    }
}
